Consider the following C function:int f(int n){static int i = 1;if(n &...
answer is 7.as,
f(1):n=2,i=2
f(2):n=4,i=3
f(4):n=7,i=4
f(7):print(n)===>>> 7<ans>
View all questions of this test
Consider the following C function:int f(int n){static int i = 1;if(n &...
Explanation:
The given function f(int n) takes an integer n as input and returns an integer value.
The function uses a static variable i which is initialized to 1 and is used in the calculation of the returned value.
The function first checks if the input value n is equal to 5. If it is, then the function returns the value 5.
If the input value n is not equal to 5, then the function multiplies n with the value of i and updates the value of i by incrementing it by 1.
After this, the function calls itself recursively with the updated value of n.
Finally, the function returns the value returned by the recursive call.
Now, let's see what happens when we call the function with the input value 1, i.e. f(1).
- The function checks if the input value n is equal to 5. Since n is not equal to 5, the function multiplies n (which is 1) with the value of i (which is 1) and updates the value of i to 2.
- The function then calls itself recursively with the updated value of n (which is now 1*1 = 1).
- In the next iteration, the function again multiplies n (which is now 1) with the value of i (which is now 2) and updates the value of i to 3.
- The function again calls itself recursively with the updated value of n (which is now 1*2 = 2).
- This process repeats until the value of i becomes 6, at which point the function checks if the input value n is equal to 5. Since n is not equal to 5, the function multiplies n (which is now 2) with the value of i (which is now 6) and updates the value of i to 7.
- The function then calls itself recursively with the updated value of n (which is now 2*6 = 12).
- Since the input value n is not equal to 5, the function continues the recursion until the value of i becomes 11 and the input value n becomes 60 (i.e. 2*3*4*5).
- At this point, the function checks if the input value n is equal to 5. Since it is not, the function multiplies n (which is now 60) with the value of i (which is now 11) and updates the value of i to 12.
- The function then calls itself recursively with the updated value of n (which is now 60*11 = 660).
- Since the input value n is not equal to 5, the function continues the recursion until the value of i becomes 16 and the input value n becomes 277200 (i.e. 2*3*4*5*6*7*8*9*10).
- At this point, the function checks if the input value n is equal to 5. Since it is not, the function multiplies n (which is now 277200) with the value of i (which is now 16) and updates the value of i to 17.
- The function then calls itself recursively with the updated value of n (which is now 277200*16 = 4435200).
- Since the input value n is not equal to 5, the function continues
Consider the following C function:int f(int n){static int i = 1;if(n &...
Static i is not created every time you call the function
To make sure you are not studying endlessly, EduRev has designed Computer Science Engineering (CSE) study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Computer Science Engineering (CSE).